package com.mifengs.mall.widget;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.animation.LinearInterpolator;

import com.mifengs.mall.R;
import com.mifengs.mall.util.UIUtil;

import me.dm7.barcodescanner.core.ViewFinderView;

/**
 * Created by haoqi on 2017/4/23.
 */

public class CustomViewFinderView extends ViewFinderView {

    public static final int TRADE_MARK_TEXT_SIZE_SP = 11;
    public final Paint mTextPaint = new Paint();
    public final Paint mMyBorderPaint = new Paint();
    private final int mMyBorderColor = getResources().getColor(R.color.yellow);
    private final String mMyTip = getResources().getString(R.string.scan_qr);
    private final Bitmap mScanLight = BitmapFactory.decodeResource(getResources(),R.drawable.scan_light);
    private final float mScanLightHeight = UIUtil.dp2px(85);
    private static final int[] SCANNER_ALPHA = new int[]{0, 64, 128, 192, 255, 192, 128, 64};
    private int scannerAlpha;
    private int SCAN_VELOCITY = 4;
    private int scanLineTop;
    private ValueAnimator mAnimator;

    public CustomViewFinderView(Context context) {
        super(context);
        init();
    }

    public CustomViewFinderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setAntiAlias(true);
        float textPixelSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                TRADE_MARK_TEXT_SIZE_SP, getResources().getDisplayMetrics());
        mTextPaint.setTextSize(textPixelSize);
        setSquareViewFinder(true);

        //border paint

        mMyBorderPaint.setColor(mMyBorderColor);
        mMyBorderPaint.setStyle(Paint.Style.STROKE);
        mMyBorderPaint.setStrokeWidth(4);
        mMyBorderPaint.setStyle(Paint.Style.FILL);

        startScanAnimation();

    }

    @Override
    public void onDraw(Canvas canvas) {
        //super.onDraw(canvas);
        if(this.getFramingRect() != null) {
            this.drawViewFinderMask(canvas);
            this.drawViewFinderBorder(canvas);
            // this.drawLaser(canvas);
            drawScanLight(canvas);
        }
        drawTradeMark(canvas);
    }


    public void drawViewFinderMask(Canvas canvas) {
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        Rect framingRect = this.getFramingRect();
        canvas.drawRect(0.0F, 0.0F, (float)width, (float)framingRect.top, this.mFinderMaskPaint);
        canvas.drawRect(0.0F, (float)framingRect.top, (float)framingRect.left, (float)(framingRect.bottom + 1), this.mFinderMaskPaint);
        canvas.drawRect((float)(framingRect.right + 1), (float)framingRect.top, (float)width, (float)(framingRect.bottom + 1), this.mFinderMaskPaint);
        canvas.drawRect(0.0F, (float)(framingRect.bottom + 1), (float)width, (float)height, this.mFinderMaskPaint);
    }

    public void drawViewFinderBorder(Canvas canvas) {
        int wid = UIUtil.dp2px(4);
        int heg = UIUtil.dp2px(22);
        Rect framingRect = this.getFramingRect();
        Rect rect01 = new Rect(framingRect.left,framingRect.top,framingRect.left+wid,framingRect.top+heg);
        Rect rect02 = new Rect(framingRect.left+wid,framingRect.top,framingRect.left+heg,framingRect.top+wid);

        Rect rect03 = new Rect(framingRect.right-heg,framingRect.top,framingRect.right,framingRect.top+wid);
        Rect rect04 = new Rect(framingRect.right-wid,framingRect.top+wid,framingRect.right,framingRect.top+heg);

        Rect rect05 = new Rect(framingRect.left,framingRect.bottom-heg,framingRect.left+wid,framingRect.bottom);
        Rect rect06 = new Rect(framingRect.left+wid,framingRect.bottom-wid,framingRect.left+heg,framingRect.bottom);

        Rect rect07 = new Rect(framingRect.right-heg,framingRect.bottom-wid,framingRect.right,framingRect.bottom);
        Rect rect08 = new Rect(framingRect.right-wid,framingRect.bottom-heg,framingRect.right,framingRect.bottom-wid);

        canvas.drawRect(rect01,mMyBorderPaint);
        canvas.drawRect(rect02,mMyBorderPaint);
        canvas.drawRect(rect03,mMyBorderPaint);
        canvas.drawRect(rect04,mMyBorderPaint);
        canvas.drawRect(rect05,mMyBorderPaint);
        canvas.drawRect(rect06,mMyBorderPaint);
        canvas.drawRect(rect07,mMyBorderPaint);
        canvas.drawRect(rect08,mMyBorderPaint);

    }

    public void drawLaser(Canvas canvas) {
        Rect framingRect = this.getFramingRect();
        this.mLaserPaint.setAlpha(SCANNER_ALPHA[this.scannerAlpha]);
        this.scannerAlpha = (this.scannerAlpha + 1) % SCANNER_ALPHA.length;
        int middle = framingRect.height() / 2 + framingRect.top;
        canvas.drawRect((float)(framingRect.left + 2), (float)(middle - 1), (float)(framingRect.right - 1), (float)(middle + 2), this.mLaserPaint);
        this.postInvalidateDelayed(80L, framingRect.left - 10, framingRect.top - 10, framingRect.right + 10, framingRect.bottom + 10);
    }


    private void drawTradeMark(Canvas canvas) {
        Rect framingRect = getFramingRect();
        float tradeMarkTop;
        float tradeMarkLeft;
        if (framingRect != null) {
            tradeMarkTop = framingRect.bottom + mTextPaint.getTextSize() + UIUtil.dp2px(20);
            float textlg = mTextPaint.measureText(mMyTip);
            tradeMarkLeft = framingRect.left + (framingRect.right-framingRect.left-textlg)/2;   //居中
        } else {
            tradeMarkTop = UIUtil.dp2px(20);
            tradeMarkLeft = canvas.getHeight() - mTextPaint.getTextSize() - 10;
        }
        canvas.drawText(mMyTip, tradeMarkLeft, tradeMarkTop, mTextPaint);
    }


    private void drawScanLight(Canvas canvas){
        Rect framingRect = getFramingRect();
        if (scanLineTop == 0) {
            scanLineTop = framingRect.top;
        }

        if (scanLineTop >= framingRect.bottom + mScanLightHeight+50) {
            scanLineTop = framingRect.top;
        } else {
            scanLineTop += SCAN_VELOCITY;
        }
        canvas.save();
        canvas.clipRect(framingRect);
        Rect scanRect = new Rect(framingRect.left, scanLineTop-(int)mScanLightHeight-50, framingRect.right,
                scanLineTop-50);
        canvas.drawBitmap(mScanLight, null, scanRect, mMyBorderPaint);
        canvas.restore();

    }


    public void startScanAnimation(){
        mAnimator = ObjectAnimator.ofInt(0, 150);
        mAnimator.setInterpolator(new LinearInterpolator());
        mAnimator.setDuration(Integer.MAX_VALUE);//持续时间
        mAnimator.start();
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                invalidate();
            }
        });
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(mAnimator!=null){
            mAnimator.cancel();
        }
    }
}
